package com.tuniu.agents.common.util;


import com.tuniu.agents.common.enums.MarkdownActionEnum;
import org.springframework.util.StringUtils;

import java.util.Map;
import java.util.stream.Collectors;

public class MarkdownActionUtil {

    /**
     * Build a Markdown formatted action link
     * 
     * @param content The text content displayed in the link
     * @param actionEnum Action enumeration
     * @param param Parameter Map
     * @return Markdown formatted link
     */
    public static String buildMarkdownAction(String content, MarkdownActionEnum actionEnum, Map<String, Object> param) {
        if (!StringUtils.hasText(content)) {
            return "";
        }
        if (actionEnum == null) {
            return content;
        }
        
        StringBuilder actionUrl = new StringBuilder();
        actionUrl.append(actionEnum.getProtocol()).append("://");
        
        if (actionEnum.getAction() != null && !actionEnum.getAction().isEmpty()) {
            actionUrl.append("action=").append(actionEnum.getAction());
        }
        
        if (param != null && !param.isEmpty()) {
            String paramString = param.entrySet().stream()
                    .map(entry -> entry.getKey() + "=" + entry.getValue())
                    .collect(Collectors.joining("&"));
            
            if (actionUrl.toString().contains("=")) {
                actionUrl.append("&");
            }
            
            actionUrl.append(paramString);
        }
        
        return String.format("[%s](%s)", content, actionUrl.toString());
    }

    /**
     * Build a Markdown formatted action link
     * 
     * @param content The text content displayed in the link
     * @param actionEnum Action enumeration
     * @param paramString JSON string
     * @return Markdown formatted link
     */
    public static String buildMarkdownAction(String content, MarkdownActionEnum actionEnum, String paramString) {
        return content;
//        if (!StringUtils.hasText(content)) {
//            return "";
//        }
//        if (actionEnum == null) {
//            return content;
//        }
//
//        StringBuilder actionUrl = new StringBuilder();
//        actionUrl.append(actionEnum.getProtocol()).append("://");
//
//        if (actionEnum.getAction() != null && !actionEnum.getAction().isEmpty()) {
//            actionUrl.append("action=").append(actionEnum.getAction());
//        }
//
//        if (paramString != null && !paramString.isEmpty()) {
//            actionUrl.append("&param=").append(paramString);
//        }
//
//        return String.format("[%s](%s)", content, actionUrl.toString());
    }

}
